A page fault happens when the required page is not found in memory. The system then brings that page from disk into RAM.
Page Replacement Algorithms are used in virtual memory systems to decide which page should be removed from main memory when a new page needs to be loaded and memory is full.
FIFO (First In First Out) Page Replacement Algorithm is one of the simplest memory management techniques used by an operating system to handle page faults. In this algorithm, the page that enters the memory first is removed first when new space is required.
Figure: FIFO Page Replacement Solved Example
FIFO stands for First In First Out. According to this method, the oldest page present in main memory is replaced whenever a new page arrives and no free frame is available. The algorithm does not consider how frequently or recently a page is used.
Initially, all page frames are empty. When pages are accessed one by one, a page fault occurs whenever the required page is not present in memory. If memory is full, FIFO removes the page that was loaded earliest.
Page Fault Rate = Number of Page Faults / Number of Frames
= 12 / 3 = 4.0
FIFO page replacement algorithm removes the oldest page from memory when a new page arrives and memory is full. In the given example with three frames, FIFO produces 12 page faults and 3 page hits.
LRU removes the page that has not been used for the longest time. It is based on recent usage patterns.
Figure: LRU Page Replacement Solved Example
In this example, we are given a page reference string and a fixed number of page frames. The task is to calculate the total number of page faults using the Least Recently Used (LRU) Page Replacement Algorithm. LRU replaces the page that has not been used for the longest period of time.
Initially, all memory frames are empty. As pages are referenced one by one, they are loaded into the available frames. Whenever a referenced page is not found in memory, a page fault occurs.
Once all four frames become full and a new page needs to be loaded, the LRU algorithm checks which page was used least recently and replaces that page with the new one. This decision is based entirely on past usage history.
For example, when page 4 is referenced and all frames are already occupied, the algorithm removes the page that was accessed farthest in the past. This process continues for every page reference until the entire reference string is processed.
The page fault rate is calculated by dividing the number of page faults by the total number of page references.
This example clearly shows how the LRU page replacement algorithm works by tracking recent page usage. LRU generally performs better than FIFO because it considers actual usage behavior, reducing unnecessary page replacements.
Optimal algorithm removes the page that will not be used for the longest time in the future. It gives minimum page faults but is not practical in real systems.
Figure: Optimal Page Replacement Solved Example
In this example, we calculate the number of page faults using the Optimal Page Replacement Algorithm. This algorithm replaces the page that will not be used for the longest period of time in the future. Because it has complete knowledge of future page references, it provides the minimum possible number of page faults.
Initially, all four frames are empty. As pages are requested one by one, they are loaded into the available frames. Every time a page is not found in memory, a page fault occurs.
Once the frames are full and a new page needs to be loaded, the Optimal algorithm checks all pages currently in memory and looks ahead in the reference string to determine which page will be used farthest in the future. That page is selected for replacement.
For example, when page 4 is referenced and memory is already full, the algorithm compares the future usage of existing pages and removes the page that will not be needed for the longest time. This decision minimizes future page faults.
This process continues for each page reference until the entire reference string is processed.
The page fault rate gives an idea of how frequently page faults occur during execution.
This example demonstrates why the Optimal Page Replacement Algorithm produces the lowest possible number of page faults. However, it is mainly used for theoretical analysis because predicting future page references is not practical in real operating systems.
MRU removes the most recently used page. It works well only in specific access patterns.
Figure: MRU Page Replacement Solved Example
In this example, we are given a page reference string and a fixed number of memory frames. Our objective is to calculate the total number of page faults, page fault rate, and page fault ratio during execution.
Initially, all memory frames are empty. When the first few pages are accessed, they are placed into the available frames. Since the pages are not already present in memory, each of these accesses causes a page fault.
Once all four frames are filled, every new page request is checked against the pages currently stored in memory. If the requested page is already available, it is counted as a page hit. If it is not present, a page fault occurs and one of the existing pages is replaced according to the page replacement rule used in the example.
This process continues for each page reference in the sequence. Every miss is counted as a page fault, and every successful access is counted as a hit.
The rate and ratio of page faults help us understand how efficiently memory is being used.
This example clearly shows how page faults occur when the required page is not present in memory. By analyzing page faults and hit ratios, we can evaluate the effectiveness of a page replacement strategy and understand how memory performance impacts overall system efficiency.
| Algorithm | Replacement Basis | Performance | Complexity |
|---|---|---|---|
| FIFO | Arrival Time | Low | Simple |
| LRU | Recent Usage | High | Complex |
| Optimal | Future Use | Best | Theoretical |
| MRU | Recent Access | Limited | Simple |